package com.knife.lov.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.knife.common.exception.CommonException;
import com.knife.lov.app.service.CodeValueService;
import com.knife.lov.domain.entity.KnifeCode;
import com.knife.lov.domain.entity.KnifeCodeValue;
import com.knife.lov.infra.cache.v2.KnifeCodeCache;
import com.knife.lov.infra.cache.v2.KnifeCodeValueCache;
import com.knife.lov.infra.mapper.KnifeCodeValueMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * @author lth
 * @created 2025年08月08日 星期五 8:00
 */
@Service
public class CodeValueServiceImpl implements CodeValueService {
    @Resource
    private KnifeCodeValueMapper knifeCodeValueMapper;
    @Resource
    private KnifeCodeValueCache knifeCodeValueCache;
    @Resource
    private KnifeCodeCache knifeCodeCache;


    @Override
    public Page<KnifeCodeValue> selectCodeValuePage(KnifeCodeValue knifeCodeValue, Page<KnifeCodeValue> pageRequest) {
        LambdaQueryWrapper<KnifeCodeValue> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .eq(KnifeCodeValue::getCodeId, knifeCodeValue.getCodeId())
                .like(StringUtils.isNotEmpty(knifeCodeValue.getValue()), KnifeCodeValue::getValue, knifeCodeValue.getValue())
                .like(StringUtils.isNotEmpty(knifeCodeValue.getMeaning()), KnifeCodeValue::getMeaning, knifeCodeValue.getMeaning())
                .like(StringUtils.isNotEmpty(knifeCodeValue.getDescription()), KnifeCodeValue::getDescription, knifeCodeValue.getDescription())
                .eq(KnifeCodeValue::getTenantId,knifeCodeValue.getTenantId())
                .orderByAsc(KnifeCodeValue::getOrderSeq)
        ;
        return knifeCodeValueMapper.selectPage(pageRequest, knifeCodeLambdaQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeCodeValue saveAndUpdateCodeValue(KnifeCodeValue knifeCodeValue) {
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeCodeValue> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .eq(KnifeCodeValue::getCodeId, knifeCodeValue.getCodeId())
                .eq(KnifeCodeValue::getValue, knifeCodeValue.getValue())
        ;
        KnifeCodeValue db = knifeCodeValueMapper.selectOne(knifeCodeLambdaQueryWrapper);


        if (Objects.isNull(db)) {
            db = new KnifeCodeValue();
        }
        // 替换数据
        db.setDescription(knifeCodeValue.getDescription());
        db.setTenantId(knifeCodeValue.getTenantId());
        db.setEnabledFlag(knifeCodeValue.getEnabledFlag());
        db.setCodeId(knifeCodeValue.getCodeId());
        db.setMeaning(knifeCodeValue.getMeaning());
        db.setValue(knifeCodeValue.getValue());
        db.setOrderSeq(knifeCodeValue.getOrderSeq());
        db.setTag(knifeCodeValue.getTag());
        if (Objects.isNull(db.getCodeValueId())) {
            knifeCodeValueMapper.insert(db);
        } else {
            knifeCodeValueMapper.updateById(db);
        }
        // 保存缓存
        knifeCodeValueCache.setCache(db);
        return db;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KnifeCodeValue deleteCodeValue(KnifeCodeValue knifeCode) {
        // 先判断这个代码在库中是否存在
        LambdaQueryWrapper<KnifeCodeValue> knifeCodeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        knifeCodeLambdaQueryWrapper
                .eq(KnifeCodeValue::getValue, knifeCode.getValue())
                .eq(KnifeCodeValue::getCodeId, knifeCode.getCodeId())
        ;
        KnifeCodeValue db = knifeCodeValueMapper.selectOne(knifeCodeLambdaQueryWrapper);
        if (Objects.isNull(db)) {
            throw new CommonException("删除的数据不存在");
        }
        knifeCodeValueMapper.deleteById(db);
        knifeCodeValueCache.deleteCache(db);
        return db;
    }

    @Override
    public List<KnifeCodeValue> selectCodeValueByCode(Long tenantId, String code) {
        KnifeCode param = new KnifeCode();
        param.setCode(code);
        param.setTenantId(tenantId);
        KnifeCode codeCache = knifeCodeCache.getCache(param);
        if (Objects.nonNull(codeCache)){
            Long codeId = codeCache.getCodeId();
            KnifeCodeValue knifeCodeValue = new KnifeCodeValue();
            knifeCodeValue.setTenantId(tenantId);
            knifeCodeValue.setCodeId(codeId);
            List<KnifeCodeValue> groupCache = knifeCodeValueCache.getGroupCache(knifeCodeValue);
            groupCache.sort(Comparator.comparing(KnifeCodeValue::getOrderSeq));
            return groupCache;
        }
        return Collections.emptyList();
    }
}
